package com.aaron.registry.zk;

import com.aaron.registry.ServiceRegistry;
import com.aaron.registry.zk.util.CuratorUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.curator.framework.CuratorFramework;

import java.net.InetSocketAddress;

// 基于zookeeper实现服务注册.得到一个持久化的节点
@Slf4j
public class ZkServiceRegistryImpl implements ServiceRegistry {

    @Override
    public void registerService(String rpcServiceName, InetSocketAddress inetSocketAddress) {
        // ZK_REGISTER_ROOT_PATH = "/my-rpc"，servicePath="/my-rpc/rpcServiceName+套接字地址"
        // 例：servicePath=/my-rpc/com.aaron.HelloServicetest2version2/192.168.5.1:8081
        String servicePath = CuratorUtils.ZK_REGISTER_ROOT_PATH + "/" + rpcServiceName + inetSocketAddress.toString();
        // 创建一个zk客户端
        CuratorFramework zkClient = CuratorUtils.getZkClient();
        // 创建持久的zookeeper节点，给一个服务路径
        CuratorUtils.createPersistentNode(zkClient, servicePath);
    }
}
